package com.raft.kvstore.client;

import com.raft.core.service.AddNodeCommand;
import com.raft.core.service.RemoveNodeCommand;
import com.raft.core.service.ServerRouter;
import com.raft.kvstore.message.GetCommand;
import com.raft.kvstore.message.SetCommand;

/**客户端和单个节点之间使用SocketChannel通信，在SocketChannel之上增加了ServerRoute用于自动选择Leader节点，最上面是内部的客户端Client
 * @author zhangsulei
 *
 */
public class Client {

    /**版本
     */
    public static final String VERSION = "0.1.0";
    /**服务器路由器
     */
    private final ServerRouter serverRouter;

    public Client(ServerRouter serverRouter) {
        this.serverRouter = serverRouter;
    }

    /**增加节点
     * @param nodeId
     * @param host
     * @param port
     */
    public void addNode(String nodeId, String host, int port) {
        serverRouter.send(new AddNodeCommand(nodeId, host, port));
    }

    /**移除节点
     * @param nodeId
     */
    public void removeNode(String nodeId) {
        serverRouter.send(new RemoveNodeCommand(nodeId));
    }

    /**kv服务set命令
     * @param key
     * @param value
     */
    public void set(String key, byte[] value) {
        serverRouter.send(new SetCommand(key, value));
    }

    /**kv服务get命令
     * @param key
     * @return
     */
    public byte[] get(String key) {
        return (byte[]) serverRouter.send(new GetCommand(key));
    }

    /**获取内部服务器路由器
     * @return
     */
    public ServerRouter getServerRouter() {
        return serverRouter;
    }

}
